using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace QFramework.Example
{
    public class CoinUpgradeSystem : AbstractSystem, IcanSave
    {
        public static EasyEvent OnCoinUpgradeSystemChanged = new EasyEvent();

        private Dictionary<string, int> dic_UpdragePrice = new Dictionary<string, int>(){
            {"add_exp",5},
            {"add_coin",5},
            {"add_maxHp",30}
        };

        public List<CoinUpgradeItem> list_CoinUpgradeItems { get; } = new List<CoinUpgradeItem>();

        public Dictionary<string, CoinUpgradeItem> dic_CoinUpgradeItems { get; } = new Dictionary<string, CoinUpgradeItem>();

        public CoinUpgradeItem Add(CoinUpgradeItem item)
        {
            list_CoinUpgradeItems.Add(item);
            return item;
        }

        public void Load()
        {
            var saveSystem = this.GetSystem<SaveSystem>();
            foreach (var item in list_CoinUpgradeItems)
            {
                item.UpgradeFinish = saveSystem.OnLoadBool(item.Key, false);
            }
        }

        public void Save()
        {
            var saveSystem = this.GetSystem<SaveSystem>();
            foreach (var item in list_CoinUpgradeItems)
            {
                saveSystem.OnSaveBool(item.Key, item.UpgradeFinish);
            }
        }

        protected override void OnInit()
        {
            
            OnRefreshCoinPanelOneAction();
        }

        private void OnRefreshCoinPanelOneAction()
        {
            dic_CoinUpgradeItems.Add("add_exp", new CoinUpgradeItem()
            .OnKey("add_exp")
            .OnDescription("当前概率：" + Mathf.FloorToInt(Global.ExpDropPrecent.Value * 100)
                + "%" + " 增加经验掉落概率  " + dic_UpdragePrice["add_exp"] + "金币")
            .OnPrice(dic_UpdragePrice["add_exp"])
            .OnUpgrade((item) =>
            {
                if (Global.Coin.Value - item.Price < 0)
                    return;
                Global.ExpDropPrecent.Value += 0.01f;
                Global.Coin.Value -= item.Price;
                item.OnDescription("当前概率：" + Mathf.FloorToInt(Global.ExpDropPrecent.Value * 100)
                + "%" + " 增加经验掉落概率  " + item.Price + "金币");
            }));


            dic_CoinUpgradeItems.Add("add_coin", new CoinUpgradeItem()
            .OnKey("add_coin")
            .OnDescription("当前概率：" + Mathf.FloorToInt(Global.CoinDropPrecent.Value * 100)
                + "%" + " 增加金币掉落概率 " + dic_UpdragePrice["add_coin"] + "金币")
            .OnPrice(dic_UpdragePrice["add_coin"])
            .OnUpgrade((item) =>
            {
                if (Global.Coin.Value - item.Price < 0)
                    return;
                Global.CoinDropPrecent.Value += 0.01f;
                Global.Coin.Value -= item.Price;
                item.OnDescription("当前概率：" + Mathf.FloorToInt(Global.CoinDropPrecent.Value * 100)
                + "%" + " 增加金币掉落概率 " + item.Price + "金币");
            }));

            dic_CoinUpgradeItems.Add("add_maxHp", new CoinUpgradeItem()
          .OnKey("add_maxHp")
          .OnDescription("当前MaxHp：" + Mathf.FloorToInt(Global.PlayerMaxHp.Value)
              + " 增加最大生命值  " + Mathf.FloorToInt(dic_UpdragePrice["add_maxHp"]) + "金币")
          .OnPrice(dic_UpdragePrice["add_maxHp"])
          .OnUpgrade((item) =>
          {
              if (Global.Coin.Value - item.Price < 0)
                  return;
              Global.PlayerMaxHp.Value += 1;
              Global.Coin.Value -= item.Price;
              item.OnDescription("当前MaxHp：" + Mathf.FloorToInt(Global.PlayerMaxHp.Value)
              + " 增加最大生命值  " + item.Price + "金币");
          }));
        }


    }

}
